#include "Queue.h"

QueueNode* BuyNode(QueueDataType x)
{
	QueueNode* tmp = (QueueNode*)malloc(sizeof(QueueNode));
	if (tmp == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	tmp->btnode = x;
	tmp->next = NULL;

	return tmp;

}

void InitQueue(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
}

void QueuePush(Queue* pq, QueueDataType x)
{
	assert(pq);
	if (pq->head == NULL)
	{
		pq->head = pq->tail = BuyNode(x);
	}
	else
	{
		pq->tail->next = BuyNode(x);
		pq->tail = pq->tail->next;
	}
}

QueueDataType QueuePop(Queue* pq)
{
	assert(pq);
	assert(!IsQueueEmpty(pq));
	QueueDataType ret = pq->head->btnode;
	QueueNode* _next = pq->head->next;
	free(pq->head);
	pq->head = _next;
	if (_next == NULL)
		pq->tail = NULL;
	return ret;
}

bool IsQueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head == NULL;
}

void QueueDestory(Queue* pq)
{
	assert(pq);
	while (!IsQueueEmpty(pq))
	{
		QueuePop(pq);
	}
}